// 实现lodash的get函数
export function get(obj: any, path: string, defaultValue?: any) {
  const travel = (regexp: RegExp) =>
    String.prototype.split
      .call(path, regexp)
      .filter(Boolean)
      .reduce(
        (res, key) => (res !== null && res !== undefined ? res[key] : res),
        obj
      );
  const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
  return result === undefined || result === obj ? defaultValue : result;
}

// 实现lodash的pick函数
export function pick(obj: any, keys: string[]) {
  return keys.reduce((acc: any, key) => {
    acc[key] = obj[key];
    return acc;
  }, {});
}
// 实现Lodash的omit函数
export function omit(obj: any, keys: string[]) {
  return Object.keys(obj).reduce((acc: any, key) => {
    if (!keys.includes(key)) {
      acc[key] = obj[key];
    }
    return acc;
  }, {});
}

export default {
  get,
  pick,
  omit,
};
